package com.czl.app.service.impl;

import com.czl.app.dto.FreeRoomDTO;
import com.czl.app.dto.RoomOrderDTO;
import com.czl.app.dto.RoomOrderDetailDTO;
import com.czl.app.mapper.FreeRoomCelendarMapper;
import com.czl.app.mapper.RoomOrderDetailMapper;
import com.czl.app.mapper.RoomOrderMapper;
import com.czl.app.mapper.RoomTypeMapper;
import com.czl.app.model.FreeRoomCelendar;
import com.czl.app.model.RoomOrder;
import com.czl.app.model.RoomOrderDetail;
import com.czl.app.model.RoomPrice;
import com.czl.app.model.RoomType;
import com.czl.app.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Component
@Service("roomService")
@Slf4j
public class RoomServiceImpl implements RoomService {
    @Resource
    private FreeRoomCelendarMapper freeRoomCelendarMapper;

    @Resource
    private RoomOrderMapper roomOrderMapper;

    @Resource
    private RoomOrderDetailMapper roomOrderDetailMapper;

    @Resource
    private RoomTypeMapper roomTypeMapper;

    @Override
    public List<FreeRoomDTO> findFreeRoomByMinFreeCount(String checkInDate, String checkOutDate) {
        List<FreeRoomCelendar> freeRoom = freeRoomCelendarMapper.findFreeRoomByMinFreeCount(checkInDate, checkOutDate);
        freeRoomCelendarMapper.findFreeRoomByMinFreeCount(checkInDate, checkOutDate);
        List<FreeRoomDTO> freeRoomDTOS=new ArrayList<>();
           for(FreeRoomCelendar freeRoomlist : freeRoom) {
                FreeRoomDTO freeRoomDTO=new FreeRoomDTO();
                Long roomTypeNum = freeRoomlist.getRoomTypeNum();
               //根据房间类型主键查询房间类型表
                RoomType roomTypeByRoomTypeNum = findRoomTypeByRoomTypeNum(roomTypeNum);
                //根据房间类型主键查询房间价格表
                RoomPrice roomPriceByRoomTypeNum = findRoomPriceByRoomTypeNum(roomTypeNum);
                freeRoomDTO.setFreeCount(freeRoomlist.getFreeCount());
                freeRoomDTO.setBedType(roomTypeByRoomTypeNum.getBedType());
                freeRoomDTO.setBreakfastType(roomPriceByRoomTypeNum.getBreakfastType());
                freeRoomDTO.setRoomPrice(roomPriceByRoomTypeNum.getRoomPrice());
                freeRoomDTO.setRoomPriceName(roomPriceByRoomTypeNum.getRoomPriceName());
                freeRoomDTO.setRoomTypeName(roomTypeByRoomTypeNum.getRoomTypeName());
                freeRoomDTOS.add(freeRoomDTO);
          }
        return freeRoomDTOS;
    }

    @Override
    public RoomType findRoomTypeByRoomTypeNum(Long roomTypeNum) {
        return freeRoomCelendarMapper.findRoomTypeByRoomTypeNum(roomTypeNum);
    }

    @Override
    public RoomPrice findRoomPriceByRoomTypeNum(Long roomTypeNum) {
        return freeRoomCelendarMapper.findRoomPriceByRoomTypeNum(roomTypeNum);
    }

    @Override
    @Transactional
    public Long reserveRoom(RoomOrderDTO roomOrderDTO) {
        RoomOrder  roomOrder=new RoomOrder();
        String customerName = roomOrderDTO.getCustomerName();
        roomOrder.setCustomerName(customerName);
        roomOrder.setCustomerPhone(roomOrderDTO.getCustomerPhone());
        roomOrder.setRoomOrderState(Byte.parseByte("0"));
        roomOrderMapper.insert(roomOrder);
        RoomOrderDetail roomOrderDetail=new RoomOrderDetail();
        int index=0;
        String checkInDateStr=null;
        String checkOutDateStr=null;
        //订房间数
        Integer roomCount=0;
        //房间类型
        Long roomTypeNum=null;
        //获取生成的订单的流水号
        Long roomOrderNum=roomOrderMapper.selectRoomOrderNumByCustomerName(customerName);
        for(RoomOrderDetailDTO roomOrderDetailDTOS : roomOrderDTO.getRoomOrderDetailDTOList()) {
                //字符串转为日期格式
                checkInDateStr=roomOrderDetailDTOS.getCheckInDate();
                checkOutDateStr=roomOrderDetailDTOS.getCheckOutDate();

                roomTypeNum=roomTypeMapper.findRoomTypeNumByRoomTypeName(roomOrderDetailDTOS.getRoomTypeName());
                roomOrderDetail.setRoomTypeNum(roomTypeNum);

                roomOrderDetail.setRoomOrderNum(roomOrderNum);
                roomOrderDetail.setRoomPriceName(roomOrderDetailDTOS.getRoomPriceName());
                roomOrderDetail.setBreakfastType(roomOrderDetailDTOS.getBreakfastType());
                roomOrderDetail.setRoomPrice(roomOrderDetailDTOS.getRoomPrice());

                roomCount=roomOrderDetailDTOS.getRoomCount();
                roomOrderDetail.setRoomCount(roomCount);

                roomOrderDetail.setCheckInDate(getNowDate(checkInDateStr));
                roomOrderDetail.setCheckOutTime(getNowDate(checkOutDateStr));
                roomOrderDetail.setRoomOrderDetailPrice(roomOrderDetailDTOS.getRoomPrice()*roomOrderDetailDTOS.getRoomCount());
                index = roomOrderDetailMapper.insert(roomOrderDetail);
                System.out.println("index---?>"+index);
        }
            if (index==1){
                //修改客房余数
                freeRoomCelendarMapper.updFreeCountByRoomCount(checkInDateStr,checkOutDateStr,roomCount,roomTypeNum);
            }
            return roomOrderNum;
    }

    /**
     * 字符串转为日期格式
     * @param dateString 日期字符串
     * @return 日期
     */
    public  java.sql.Date getNowDate(String dateString) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Date currentTime_2 = null;
        try {
            currentTime_2 = (Date) formatter.parse(dateString);
        } catch (ParseException e) {
        }
        java.sql.Date resultDate = new java.sql.Date(currentTime_2.getTime());
        System.out.println("checkOutDateStr------>"+resultDate);
        return resultDate;
    }
}
